function example5
% compares collocation and least squares approximations for the BVP
% y'' + p(x)y' + q(x)y= f(x) for xL < x < xr '
% where
% y(xl) = yL and y(xR) = yR
% p=0, q=0, f=-4*(1-x)*exp(-2*x)
% clear all previous variables and plots
clear *
clf
% set boundary conditions
xL=0; yL=0;
xR=1; yR=xR*exp(-2*xR);
% collocation point
x1=1/2;
% get(gcf)
%set(gcf,'Position', [1203 732 515 307]);
set(gcf,'Position', [1203 757 530 280]);
% calculate and plot exact solution
a1=yL;
nx=40;
x=linspace(xL,xR,nx);
for ix=1:nx
exact(ix)=x(ix)*exp(-2*x(ix));
end;
plot(x,exact,'k','LineWidth',1)
hold on
% define title and axes used in plot
xlabel('x-axis','FontSize',14,'FontWeight','bold')
ylabel('Solution','FontSize',14,'FontWeight','bold')
title('BVP: Example 5','FontSize',14,'FontWeight','bold')
% calculate and plot collocation solution
a3=f(x1)/2;
a2=yR-yL-a3;
for ix=1:nx
coll(ix)=a1+a2*x(ix)+a3*x(ix)^2;
end;
plot(x,coll,'-r','LineWidth',1)
% calculate and plot least squares solution
a3=(-1-exp(-2))/2;
a2=yR-yL-a3;
for ix=1:nx
least(ix)=a1+a2*x(ix)+a3*x(ix)^2;
end;
plot(x,least,'-b','LineWidth',1)
legend(' Exact',' Collocation',' Least Squares',4)
% have MATLAB use certain plot options (all are optional)
% Set the fontsize to 14 for the plot
set(gca,'FontSize',14);
% Set legend font to 14/bold
set(findobj(gcf,'tag','legend'),'FontSize',14,'FontWeight','bold');
box on
hold off
function q=f(x)
q=-4*(1-x)*exp(-2*x);